package cn.stylefeng.guns.modular.manager.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.manager.entity.FlowNews;
import cn.stylefeng.guns.modular.manager.model.params.FlowNewsParam;
import cn.stylefeng.guns.modular.manager.service.FlowNewsService;
import cn.stylefeng.guns.modular.manager.service.FlowNewsTypeService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;


/**
 * 资讯中心控制器
 *
 * @author yumker
 * @Date 2021-01-07 17:22:24
 */
@Controller
@RequestMapping("/flowNews")
public class FlowNewsController extends BaseController {

    private String PREFIX = "/manager/flowNews";

    @Autowired
    private FlowNewsService flowNewsService;

    @Autowired
    private FlowNewsTypeService flowNewsTypeService;

    /**
     * 跳转到主页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/flowNews.html";
    }

    /**
     * 新增页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/flowNews_add.html";
    }

    /**
     * 编辑页面
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/flowNews_edit.html";
    }

    /**
     * 新增接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FlowNewsParam flowNewsParam) {
        if(!StringUtils.isEmpty(flowNewsParam.getDetail())){
            try {
                flowNewsParam.setDetail(URLDecoder.decode(flowNewsParam.getDetail(),"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        this.flowNewsService.add(flowNewsParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FlowNewsParam flowNewsParam) {
        if(!StringUtils.isEmpty(flowNewsParam.getDetail())){
            try {
                flowNewsParam.setDetail(URLDecoder.decode(flowNewsParam.getDetail(),"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        this.flowNewsService.update(flowNewsParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FlowNewsParam flowNewsParam) {
        this.flowNewsService.delete(flowNewsParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FlowNewsParam flowNewsParam) {
        FlowNews detail = this.flowNewsService.getById(flowNewsParam.getNewsId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author yumker
     * @Date 2021-01-07
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FlowNewsParam flowNewsParam) {
        return this.flowNewsService.findPageBySpec(flowNewsParam);
    }

    @ResponseBody
    @RequestMapping("/newsType")
    public ResponseData newsType(){
        return ResponseData.success(flowNewsTypeService.list());
    }
}


